 /*
 * @description: app开屏广告
 * @Author: along
 * @Date: 2020-09-17
 * @Last Modified by: liyujie
 * @Last Modified time: 2020-02-04
 */
<template>
    <div class="container appSide">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left"/>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="()=>{
                            this.$router.push({
                                name: 'appSideEdit',
                                query: {
                                    advertise_no: ''
                                }
                            });
                        }"
                    >
                        新建开屏广告
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="appSide"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="image_url"
                label="图片展示"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <img
                        :src="scope.row.image_url"
                        :style="{width: '50px', height: '50px'}"
                        @click="fnShowPreview(scope.row.image_url)"
                    >
                </template>
            </el-table-column>
            <el-table-column
                label="应用场景"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template>
                    APP
                </template>
            </el-table-column>
            <el-table-column
                prop="url"
                label="跳转目标"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="edit_time"
                label="上次修改时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.edit_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                label="显示状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.show === 1">已显示</div>
                    <div v-if="scope.row.show === 0">已隐藏</div>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="fnSettingCoupon(scope.row)"
                    />
                    {{ scope.row.status == 1 ? '已上架': '已下架' }}
                </template>
            </el-table-column>
            <el-table-column
                label="开始时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                label="结束时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnEditSide(scope.row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnDeleteSide(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {},
            cascadeList: []
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '开屏广告'}]);
        this.authResult = this.$route.meta.authResult;

        this.$post('/student/app_action%5Cget_cascade_list', {}, resp => {
            if (resp.code == 1) {
                this.cascadeList = resp.data.list;
                this.getTableData();
            } else {
                this.$notify({
                    title: '提示',
                    message: resp.msg,
                    type: 'warning',
                    duration: 2000
                });
            }
        });
    },
    methods: {
        /**
         * @description 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/app_advertise%5Cget_list', {
                page_id: page || this.page.now_page
            }, resp => {
                if (resp.code == 1) {

                    let list = resp.data.list;
                    let data = [];

                    if(list.length) {
                        for(let i=0; i<list.length; i++) {
                            data.push({
                                advertise_no: list[i].advertise_no,
                                image_url: list[i].image_url,
                                url: this.getUrlName(list[i].app_action),
                                status: list[i].status,
                                sort: list[i].sort,
                                edit_time: list[i].edit_time,
                                start_time: list[i].start_time,
                                end_time: list[i].end_time,
                                show: list[i].show,
                            });
                        }
                    }

                    this.tableData = data;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 获取名称
         */
        getUrlName (object) {
            if(object === null) return false;
            let str = '';

            if(object.action) {
                let action = object.action;

                for(let key in this.cascadeList) {
                    if(this.cascadeList[key].action == action) {
                        str += this.cascadeList[key].title;

                        if(this.cascadeList[key].action) {
                            let target_list = this.cascadeList[key].target_list;
                            let target = object.target;

                            for(let i in target_list) {
                                if(target_list[i].target == target) {
                                    str += '/' + target_list[i].title;

                                    return str;
                                }
                            }
                        }
                    }
                }

            }
        },

        /**
         * @description 编辑开屏广告
         */
        fnEditSide (row) {
            this.$router.push({
                name: 'appSideEdit',
                query: {
                    advertise_no: row.advertise_no
                }
            });
        },

        /**
         * @description 删除开屏广告
         */
        fnDeleteSide (row) {
            this.$confirm('是否要删除该开屏广告', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/app_advertise%5Cdelete',{
                    advertise_no: row.advertise_no
                },resp => {
                    if(resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },


        /**
         * @description 开启/关闭
         */
        fnSettingCoupon (row) {
            this.$post('/student/app_advertise%5Cset_status',{
                advertise_no: row.advertise_no,
                status: row.status
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success',
                        duration: 2000
                    });
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
.container {
    background-color: #fff;
    padding: 10px 15px 5px 15px;
    display: flex;
    flex-direction: column;
    position: relative;
    width: 100%;
    .header-nav {
        position: relative;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        .header-left, .header-right {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
        }
        .header-new-box {
            display: flex;
            margin-right: 15px;
            align-items: center;
            color: #606266;
            margin-bottom: 10px;
            white-space: nowrap;
            &-title {
                margin-right: 2px;
                white-space: nowrap;
            }
        }
    }
    /* 表单 */
    .scope-price {
        width: 50px;
        height: 50px;
    }
    /* 字体高亮 */
    .cursor-font {
        color: rgb(64, 158, 255);
        cursor: pointer;
    }
    /* 分页 */
    .pagination-wrapper {
        padding-top: 10px;
        padding-bottom: 10px;
        .el-pagination {
            float: right;
            padding: 0;
            background-color: transparent;
        }
    }
}
/* 排序 */
.sort-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    .sort-spliter {
        width: 1px;
        height: 12px;
        background: rgba(235, 235, 235, 1);
        margin: 0 10px;
        }
        .btn-sort {
        font-size: 14px;
        line-height: 1;
        color: #ccc;
        &:hover {
            color: #20a0ff;
        }
    }
}
.el-button-left {
    margin-left: 40px;
}
.el-button-right {
    margin-right: 10px;
}
</style>
<style lang="less">
.appSide{
    .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
}
</style>
